/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.web.floor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.source.entity.floor.FloorNode;
import com.thinkgem.jeesite.modules.source.service.floor.FloorNodeService;

/**
 * 楼层维护Controller
 * @author ZC
 * @version 2017-07-06
 */
@Controller
@RequestMapping(value = "${adminPath}/source/floor/floorNode")
public class FloorNodeController extends BaseController {

	@Autowired
	private FloorNodeService floorNodeService;
	
	@ModelAttribute
	public FloorNode get(@RequestParam(required=false) String id) {
		FloorNode entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = floorNodeService.get(id);
		}
		if (entity == null){
			entity = new FloorNode();
		}
		return entity;
	}
	
	@RequiresPermissions("source:floor:floorNode:view")
	@RequestMapping(value = {"list", ""})
	public String list(FloorNode floorNode, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<FloorNode> page = floorNodeService.findPage(new Page<FloorNode>(request, response), floorNode); 
		model.addAttribute("page", page);
		return "modules/source/floor/floorNodeList";
	}

	@RequiresPermissions("source:floor:floorNode:view")
	@RequestMapping(value = "form")
	public String form(FloorNode floorNode, Model model) {
		model.addAttribute("floorNode", floorNode);
		return "modules/source/floor/floorNodeForm";
	}

	@RequiresPermissions("source:floor:floorNode:edit")
	@RequestMapping(value = "save")
	public String save(FloorNode floorNode, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, floorNode)){
			return form(floorNode, model);
		}
		floorNodeService.save(floorNode);
		addMessage(redirectAttributes, "保存楼层节点成功");
		return "redirect:"+Global.getAdminPath()+"/source/floor/floorNode/?repage";
	}
	
	@RequiresPermissions("source:floor:floorNode:edit")
	@RequestMapping(value = "delete")
	public String delete(FloorNode floorNode, RedirectAttributes redirectAttributes) {
		floorNodeService.delete(floorNode);
		addMessage(redirectAttributes, "删除楼层节点成功");
		return "redirect:"+Global.getAdminPath()+"/source/floor/floorNode/?repage";
	}

}